package bd;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import model.SetorForm;
import model.StatusForm;

public class JDBCSetorDAO implements StatusDAO, SetorDAO 
{
	Connection conn;
	public Vector searchallStatus() 
	{
		StatusForm status = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		Vector<StatusForm> vecStatus = new Vector<StatusForm>();
		try
		{
			stm = conn.createStatement();
			strSQL = "select * from TAB_STATUS";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				status = new StatusForm(rs.getInt("COD_STATUS"), rs.getString("STR_DESCRICAO_STATUS"));
				System.out.println("Entrei no wile do status");
				System.out.println(strSQL);
				System.out.println(status.getDescricao_status());
				vecStatus.add(status);
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return vecStatus;
	}
	public Vector searchallsetor() 
	{
		SetorForm setor = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		Vector<SetorForm> vecSetor = new Vector<SetorForm>();
		try
		{
			stm = conn.createStatement();
			strSQL = "select * from TAB_SETOR where STR_DESABILITADO = 'FALSE'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				setor = new SetorForm(rs.getInt("COD_SETOR"), rs.getString("STR_NOME_SETOR"), rs.getString("STR_DESCRICAO_SETOR"), rs.getString("STR_DESABILITADO"), rs.getInt("COD_USUARIO"));
				System.out.println("Entrei no wile do status");
				System.out.println(strSQL);
				System.out.println(setor.getNome_setor());
				vecSetor.add(setor);
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return vecSetor;
	}
	
	/**
	 * Aqui
	 */
	
	public SetorForm findexistentesetor(String nome_setor)
	{
		SetorForm setor = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		
		try
		{
			stm = conn.createStatement();
			strSQL = "select COD_SETOR, STR_NOME_SETOR, STR_DESCRICAO_SETOR, STR_DESABILITADO, COD_USUARIO from TAB_SETOR where STR_NOME_SETOR = '"+nome_setor+"' and STR_DESABILITADO = 'FALSE'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				setor = new SetorForm(rs.getInt("COD_SETOR"), rs.getString("STR_NOME_SETOR"), rs.getString("STR_DESCRICAO_SETOR"), rs.getString("STR_DESABLITADO"), rs.getInt("COD_USUARIO"));
				System.out.println("Entrei no wile");
				System.out.println(strSQL);
				System.out.println(setor.getNome_setor());
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}	
		return setor;
	}
	public SetorForm insertSetor(SetorForm set)
	{
		SetorForm setor = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		Statement stm2;
		try {
			stm = conn.createStatement();
			stm2 = conn.createStatement();
			strSQL = "insert into TAB_SETOR (STR_NOME_SETOR, STR_DESCRICAO_SETOR, STR_DESABILITADO, COD_USUARIO) " +
					" values (" +
					"'"+ set.getNome_setor() +"', '"+ set.getDescricao_setor() +"', '"+ set.getDesabilitado() +"', '"+ set.getCod_usuario() +"')";
		    System.out.println(strSQL);
	    	stm.execute(strSQL);
	        stm.close();
	    	strSQL = "select COD_SETOR, STR_NOME_SETOR, STR_DESCRICAO_SETOR, STR_DESABILITADO, COD_USUARIO from TAB_SETOR where STR_NOME_SETOR = '"+set.getNome_setor()+"'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm2.executeQuery(strSQL);
			while (rs.next())
			{
				setor = new SetorForm(rs.getInt("COD_SETOR"), rs.getString("STR_NOME_SETOR"), rs.getString("STR_DESCRICAO_SETOR"), rs.getString("STR_DESABILITADO"), rs.getInt("COD_USUARIO"));
				System.out.println("Entrei no wile");
				System.out.println(strSQL);
			}
			rs.close();
			stm2.close();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return setor;
	}
	public SetorForm findsetor(String parameter)
	{
		SetorForm setor = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		
		try
		{
			stm = conn.createStatement();
			strSQL = "select COD_SETOR, STR_NOME_SETOR, STR_DESCRICAO_SETOR, STR_DESABILITADO, COD_USUARIO from TAB_SETOR where COD_SETOR = '"+parameter+"' and STR_DESABILITADO = 'FALSE'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				setor = new SetorForm(rs.getInt("COD_SETOR"), rs.getString("STR_NOME_SETOR"), rs.getString("STR_DESCRICAO_SETOR"), rs.getString("STR_DESABILITADO"), rs.getInt("COD_USUARIO"));
				System.out.println(strSQL);
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		
		return setor;
	}
	public SetorForm alterarSetor(SetorForm set)
	{
		SetorForm setor = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		Statement stm2;
		try {
			stm = conn.createStatement();
			stm2 = conn.createStatement();
			strSQL = "update TAB_SETOR set STR_NOME_SETOR = '"+ set.getNome_setor() +"'," +
					" STR_DESCRICAO_SETOR = '"+ set.getDescricao_setor() +"', STR_DESABILITADO = '"+ set.getDesabilitado() +"' , COD_USUARIO = '"+ set.getCod_usuario() +"'" +
					"where COD_SETOR = '"+ set.getCod_setor() +"'";
			
		    System.out.println(strSQL);
	    	stm.execute(strSQL);
	        stm.close();
	    	strSQL = "select COD_SETOR, STR_NOME_SETOR, STR_DESCRICAO_SETOR, STR_DESABILITADO, COD_USUARIO from TAB_SETOR where COD_SETOR = '"+set.getCod_setor()+"'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm2.executeQuery(strSQL);
			while (rs.next())
			{
				setor = new SetorForm(rs.getInt("COD_SETOR"), rs.getString("STR_NOME_SETOR"), rs.getString("STR_DESCRICAO_SETOR"), rs.getString("STR_DESABILITADO"), rs.getInt("COD_USUARIO"));
				System.out.println(strSQL);
				System.out.println(setor.getNome_setor());
			}
			rs.close();
			stm2.close();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return setor;
	}
	public void excluirSetor(SetorForm set) 
	{
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		try {
			stm = conn.createStatement();
			strSQL = "update TAB_SETOR set STR_DESABILITADO = '"+ set.getDesabilitado() +"'" +
					"where COD_SETOR = '"+ set.getCod_setor() +"'";	
		    System.out.println(strSQL);
	    	stm.execute(strSQL);
	        stm.close();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}



}

	
